import type { ECharts } from 'echarts'

export const setChartOptions = (chart: ECharts, xAxisData: string[], yAxisLeftMax: number, yAxisRightMax: number, yAxisRightMin: number, faultData: number[], yoyDta: number[], momData: number[]) => {
  yAxisLeftMax = yAxisLeftMax < 100 ? 100 : yAxisLeftMax
  yAxisRightMax = yAxisRightMax < 100 ? 100 : yAxisRightMax

  // 向上取整十
  yAxisLeftMax = Math.ceil(yAxisLeftMax / 10) * 10
  yAxisRightMax = Math.ceil(yAxisRightMax / 10) * 10
  yAxisRightMin = Math.floor(yAxisRightMin / 10) * 10

  yAxisRightMin = yAxisRightMin > 0 ? 0 : yAxisRightMin

  const rightInterval = (yAxisRightMax - yAxisRightMin) / 5
  const leftInterval = yAxisLeftMax / 5
  chart.setOption({
    grid: {
      top: '40px',
      left: '30px',
      bottom: '30px',
      right: '80px'
    },

    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
      // formatter: function (params: any) {
      //   return `
      //   <div>${params[0].marker} ${params[0].seriesName} <span class="bold">${params[0].value}</span> 个</div>
      //   <div>${params[1].marker} ${params[1].seriesName} <span class="bold">${params[1].value}</span> %</div>
      //   <div>${params[2].marker} ${params[2].seriesName} <span class="bold">${params[2].value}</span> %</div>
      //   `
      // }
    },

    legend: {
      data: ['故障数', '环比增长', '同比增长'],
      right: 10,
      top: 0
    },
    xAxis: {
      data: xAxisData
    },
    yAxis: [
      {
        type: 'value',
        max: yAxisLeftMax,
        min: 0,
        splitNumber: 5,
        interval: leftInterval
      },
      {
        type: 'value',
        min: yAxisRightMin,
        max: yAxisRightMax,
        splitNumber: 5,
        interval: rightInterval,
        axisLabel: {
          formatter: '{value} %'
        }
      }
    ],
    series: [
      {
        name: '故障数',
        type: 'bar',
        data: faultData,
        barWidth: 40,
        label: {
          show: true,
          position: 'top'
        }
      },
      {
        name: '环比增长',
        data: momData, // 环比
        type: 'line',
        yAxisIndex: 1
      },
      {
        name: '同比增长',
        data: yoyDta, // 同比
        type: 'line',
        yAxisIndex: 1
      }
    ]
  })
}
